// pages/insuranceList/insuranceList.js
const app = getApp()
const constant = require('../../constant');
const util = require('../../utils/util');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageNo: 1,
    pageCount: 10,
    orderInsurance: [],
    listEnd: false,
    isNodata: true,
    urlPublic: constant.config.url_public
  },
  /**
     * 查询订单
     * goodstype: 1->新车 3->保险  5->商城
     */
  getOrder: function (goodstype, pageNo = 1) {
    let that = this;
    app.get('User/myorder', {
      type: goodstype,
      page_no: pageNo,
      page_count: that.data.pageCount
    }, function (res) {
      if (res.code == 200 && res.data.order_list.length > 0) {
        for (let i = 0; i < res.data.order_list.length; i++) {
          for (let j = 0; j < res.data.order_list[i].goods_list.length; j++) {
            var goods_name = res.data.order_list[i].goods_list[j].goods_name;
            var goods_img = res.data.order_list[i].goods_list[j].goods_img;
            if (util.strlen(goods_name) > 30) {
              res.data.order_list[i].goods_list[j].goods_name = util.strmask(goods_name, 30);
            }
            res.data.order_list[i].goods_list[j].goods_img = constant.config.host + goods_img;
          }
        }
        if (goodstype == constant.define.CAT_INSURANCE) {
          var list = that.data.orderInsurance.concat(res.data.order_list);
          that.setData({ orderInsurance: list, isNodata: false });
          if (that.data.pageNo * that.data.pageCount >= res.data.total_count) {
            that.setData({ listEnd: true });
          }
        }
      }
    })
  },
  lowerInsurance: function (e) {
    if (this.data.listEnd) return;
    var pageNo = this.data.pageNo + 1;
    this.setData({ pageNo: pageNo });
    this.getOrder(constant.define.CAT_INSURANCE, pageNo);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOrder(constant.define.CAT_INSURANCE); //保险
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})